package goods_manage.show.view;

import goods_manage.entity.GoodsDo;
import goods_manage.handler.UpdateGoodsViewHandler;
import goods_manage.service.GoodsService;
import goods_manage.service.impl.GoodsServiceImpl;
import goods_manage.show.MainView;

import javax.swing.*;
import java.awt.*;

public class UpdateGoodsView extends JDialog {
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel idLabel=new JLabel("编号",JLabel.RIGHT);
    JTextField idText=new JTextField();
    JLabel nameLabel=new JLabel("商品名",JLabel.RIGHT);
    JTextField nameText=new JTextField();
    JLabel priceLabel=new JLabel("价格",JLabel.RIGHT);
    JTextField priceText=new JTextField();
    JLabel purchaseLabel =new JLabel("购进",JLabel.RIGHT);
    JTextField purchaseText =new JTextField();
    JLabel saleLabel=new JLabel("售出",JLabel.RIGHT);
    JTextField saleText=new JTextField();
    JLabel inventoryLabel=new JLabel("库存",JLabel.RIGHT);
    JTextField inventoryText=new JTextField();
    JLabel typeLabel =new JLabel("商品类型",JLabel.RIGHT);
    JTextField typeText =new JTextField();
    JButton updateBtn=new JButton("修改");

    UpdateGoodsViewHandler updateGoodsViewHandler;



    public UpdateGoodsView(MainView mainView, int selectedGoodsId){
        super(mainView,"修改商品",true);
        updateGoodsViewHandler =new UpdateGoodsViewHandler(this,mainView);
        //查询selectedStudentIds对应的记录并回显
        GoodsService goodsService =new GoodsServiceImpl();
        GoodsDo selectedGood= goodsService.getById(selectedGoodsId);



        //添加组件
        idLabel.setPreferredSize(new Dimension(80,30));
       idText.setPreferredSize(new Dimension(200,30));
        jPanel.add(idLabel);
        idText.setText(selectedGood.getId()+"");
        //设置Id不可编辑
        idText.setEnabled(false);
        jPanel.add(idText);

        nameLabel.setPreferredSize(new Dimension(80,30));
        nameText.setPreferredSize(new Dimension(200,30));
        nameText.setText(selectedGood.getName());
        jPanel.add(nameLabel);
        jPanel.add(nameText);

        priceLabel.setPreferredSize(new Dimension(80,30));
        priceText.setPreferredSize(new Dimension(200,30));
        priceText.setText(selectedGood.getPrice());
        jPanel.add(priceLabel);
        jPanel.add(priceText);

        purchaseLabel.setPreferredSize(new Dimension(80,30));
        purchaseText.setPreferredSize(new Dimension(200,30));
        purchaseText.setText(selectedGood.getPurchase());
        jPanel.add(purchaseLabel);
        jPanel.add(purchaseText);

        saleLabel.setPreferredSize(new Dimension(80,30));
        saleText.setPreferredSize(new Dimension(200,30));
        saleText.setText(selectedGood.getSale());
        jPanel.add(saleLabel);
        jPanel.add(saleText);

        inventoryLabel.setPreferredSize(new Dimension(80,30));
        inventoryText.setPreferredSize(new Dimension(200,30));
        inventoryText.setText(selectedGood.getInventory());
        jPanel.add(inventoryLabel);
        jPanel.add(inventoryText);

        typeLabel.setPreferredSize(new Dimension(80,30));
        typeText.setPreferredSize(new Dimension(200,30));
        typeText.setText(selectedGood.getType());
        jPanel.add(typeLabel);
        jPanel.add(typeText);

        updateBtn.addActionListener(updateGoodsViewHandler);
        jPanel.add(updateBtn);
        //
        Container container=getContentPane();
        container.add(jPanel);


        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    /**
     * 获取修改后的学生对象
     * @return
     */
    public GoodsDo buildUpdateGoods() {
        GoodsDo goodsDo =new GoodsDo();
        goodsDo.setName(nameText.getText());
        goodsDo.setPrice(priceText.getText());
        goodsDo.setPurchase(purchaseText.getText());
        goodsDo.setSale(saleText.getText());
        goodsDo.setInventory(inventoryText.getText());
        goodsDo.setType(typeText.getText());
        goodsDo.setId(Integer.parseInt(idText.getText()));
        return goodsDo;
    }

}
